package com.zpark;

import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamReader;
import javax.xml.stream.events.*;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Set;

/**
 * @PackageName: com.zpark
 * @ClassName: StAXParseFileDemo
 * @Description:
 * @author: RZS
 * @date: 2021/8/15  20:01
 */
public class StAXParseFileDemo {
    public static void main(String[] args) throws Exception {
        // 获取xml文件所在路径
        String path = StAXParseFileDemo.class.getClassLoader().getResource("stax.xml").getPath();
        // 由于路径包含中文，所以需要进行解码
        path = URLDecoder.decode(path, "utf-8");

        boolean bFirstName = false;
        boolean bLastName = false;
        boolean bNickName = false;
        boolean bMarks = false;

        XMLInputFactory factory = XMLInputFactory.newInstance();
        XMLEventReader eventReader = factory.createXMLEventReader(new FileReader(path));

        while(eventReader.hasNext()){
            XMLEvent event = eventReader.nextEvent();
            switch(event.getEventType()){
                case XMLStreamConstants.START_ELEMENT:
                    StartElement startElement = event.asStartElement();
                    String qName = startElement.getName().getLocalPart();
                    if (qName.equalsIgnoreCase("student")) {
                        System.out.println("Start Element : student");
                        Iterator<Attribute> attributes = startElement.getAttributes();
                        String rollNo = attributes.next().getValue();
                        System.out.println("Roll No : " + rollNo);
                    } else if (qName.equalsIgnoreCase("firstname")) {
                        bFirstName = true;
                    } else if (qName.equalsIgnoreCase("lastname")) {
                        bLastName = true;
                    } else if (qName.equalsIgnoreCase("nickname")) {
                        bNickName = true;
                    }
                    else if (qName.equalsIgnoreCase("marks")) {
                        bMarks = true;
                    }
                    break;
                case XMLStreamConstants.CHARACTERS:
                    Characters characters = event.asCharacters();
                    if(bFirstName){
                        System.out.println("First Name: "
                                + characters.getData());
                        bFirstName = false;
                    }
                    if(bLastName){
                        System.out.println("Last Name: "
                                + characters.getData());
                        bLastName = false;
                    }
                    if(bNickName){
                        System.out.println("Nick Name: "
                                + characters.getData());
                        bNickName = false;
                    }
                    if(bMarks){
                        System.out.println("Marks: "
                                + characters.getData());
                        bMarks = false;
                    }
                    break;
                case  XMLStreamConstants.END_ELEMENT:
                    EndElement endElement = event.asEndElement();
                    if(endElement.getName().getLocalPart().equalsIgnoreCase("student")){
                        System.out.println("End Element : student");
                        System.out.println();
                    }
                    break;
            }
        }
    }
}
